#include "ReadVideoThread.h"

// Qt includes
#include <QtCore/QTimer>

// Local includes
#include "IpcClient.h"
#include "Video.h"

ReadVideoThread::ReadVideoThread(Video *video, QObject *parent)
 : QThread(parent), mVideo(video), mReadTimer(0)
{
}

void ReadVideoThread::run()
{
	exec();
}

void ReadVideoThread::setIpcClient(IpcClient *client)
{
	mIpcClient = client;
	connect(mIpcClient, SIGNAL(serverAcknowledged()), this, SIGNAL(gotNewFrame()));
}

void ReadVideoThread::startRead()
{
	if( !mReadTimer )
	{
		mReadTimer = new QTimer(this);
		connect(mReadTimer, SIGNAL(timeout()), this, SLOT(readFrame()));
	}

	mReadTimer->start(30);
}

void ReadVideoThread::pauseRead()
{
	mReadTimer->stop();
}

void ReadVideoThread::stopRead()
{
	mReadTimer->stop();
}

void ReadVideoThread::readFrame()
{
	mVideo->readFrame();

	mIpcClient->sendCommand("DO_FILTER");
}
